/**
 * @file     isPerfectSquare.js
 * @brief    [367. 有效的完全平方数](https://leetcode.cn/problems/valid-perfect-square/description/)
 * @author   Zhu
 * @date     2023-02-13 23:24
 */

/**
 * 二分
 * @param {number} num
 * @return {boolean}
 */
var isPerfectSquare = function (num) {
    let left = 1;
    let right = num;
    let mid;
    do {
        mid = Math.floor(left / 2 + right / 2);
        result = mid * mid;
        if (result < num) {
            left = mid + 1;
        } else if (result > num) {
            right = mid;
        } else {
            return true;
        }
    } while (left < right);

    return false;
};

/**
 * 暴力枚举
 * @param {number} num
 * @return {boolean}
 */
var isPerfectSquare = function (num) {
    let i = 1;
    let target;
    do {
        target = i * i;
        if (target === num) return true;
        i++;
    } while (target < num);

    return false;
};
